package com.fh.job;

import com.fh.model.loan.CallLoanInfo;
import com.fh.model.loan.CallLoanSysLog;
import com.fh.service.cuijimanage.CuijimanageServiceImpl;
import com.fh.util.mail.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell、 on 2017/5/17.
 */
public class CuijiMessageActionJob {
    private Logger logger = LoggerFactory.getLogger(CuijiMessageActionJob.class);

    @Value("${url.cuijiDays}")
    private String cuijiDays;
    @Autowired
    private CuijimanageServiceImpl cuijimanageServiceImpl;
    protected void execute() {
        logger.info("崔姬消息定时器执行---------------");
        if(cuijiDays==null){
        	cuijiDays="3";
        }
        List<CallLoanInfo> listCallLoanInfo = cuijimanageServiceImpl.findAllListdaiandzhong();
        long cuijiDays1 = Long.valueOf(cuijiDays);
        for(CallLoanInfo callLoanInfo:listCallLoanInfo){
            logger.info("扫描执行callloanid:{}---------------",callLoanInfo.getCallLoanNid());
            //未分派任务
            if(callLoanInfo.getIsCall()==0) {
                //本地入库时间超过三天
                Date callLoanInfoCreateTime = callLoanInfo.getCreateTime();
                long days = DateUtil.getBetweenDays(new Date(), callLoanInfoCreateTime);
                if (days >= cuijiDays1) {
                    CallLoanSysLog callLoanSysLog = new CallLoanSysLog();
                    callLoanSysLog.setTitle("催收消息通知");
                    callLoanSysLog.setContent("案件编号" + callLoanInfo.getCallLoanNid() + "，已经" + cuijiDays + "天没有添加催收记录了，请尽快处理");
                    callLoanSysLog.setStatus(0);
                    callLoanSysLog.setIsDelete(0);
                    callLoanSysLog.setCallUser(callLoanInfo.getCallUser());
                    callLoanSysLog.setCreateTime(new Date());
                    callLoanSysLog.setUpdateTime(new Date());
                    cuijimanageServiceImpl.insertCuijiMsg(callLoanSysLog);
                }
            }
             //以分派任务
             if(callLoanInfo.getIsCall()==1){
                 Map<String,Object> mao = new HashMap<>();
                 mao.put("callLoanNid",callLoanInfo.getCallLoanNid());
                 Map<String,Object> maxRecorder = cuijimanageServiceImpl.findMaxCreateTimeLogList(mao);
                 //无催收记录
                 if(maxRecorder==null) {
                     //已分派任务超过三天
                     Date callLoanInfoFenpaiTime = callLoanInfo.getCallTime();
                     if (callLoanInfoFenpaiTime != null) {
                         long  days1 = DateUtil.getBetweenDays(new Date(), callLoanInfoFenpaiTime);
                         if (days1 >= cuijiDays1) {
                             CallLoanSysLog callLoanSysLog = new CallLoanSysLog();
                             callLoanSysLog.setTitle("催收消息通知");
                             callLoanSysLog.setContent("案件编号" + callLoanInfo.getCallLoanNid() + "，任务已分派" + cuijiDays + "天了，没有添加任何催收记录，请尽快处理");
                             callLoanSysLog.setStatus(0);
                             callLoanSysLog.setIsDelete(0);
                             callLoanSysLog.setCallUser(callLoanInfo.getCallUser());
                             callLoanSysLog.setCreateTime(new Date());
                             callLoanSysLog.setUpdateTime(new Date());
                             cuijimanageServiceImpl.insertCuijiMsg(callLoanSysLog);
                         }
                     }
                 }
                 //有催收记录
                 if(maxRecorder!=null){
                     //最新的催收记录时间超过三天
                     Date maxDate = (Date) maxRecorder.get("maxcreatetime");
                     long days2 = DateUtil.getBetweenDays(new Date(), maxDate);
                     if (days2 >= cuijiDays1) {
                         String callLoanNid1 = (String) maxRecorder.get("callLoanNid");
                         String toRealName = (String) maxRecorder.get("toRealName");
                         CallLoanSysLog callLoanSysLog = new CallLoanSysLog();
                         callLoanSysLog.setTitle("催收消息通知");
                         callLoanSysLog.setContent("案件编号" + callLoanNid1 + "，已经" + cuijiDays + "天没有更新催收记录了，请尽快处理" );
                         callLoanSysLog.setStatus(0);
                         callLoanSysLog.setIsDelete(0);
                         callLoanSysLog.setCallUser(callLoanInfo.getCallUser());
                         callLoanSysLog.setCreateTime(new Date());
                         callLoanSysLog.setUpdateTime(new Date());
                         cuijimanageServiceImpl.insertCuijiMsg(callLoanSysLog);
                     }
                 }
            }
        }
    }
}
